## This test checks basic functionality of glob (or "shell wildcard") matching,
## as well as verifying all the relevant flags in llvm-objcopy and llvm-strip
## are configured correctly.
## For more detailed syntax tests, see wildcard-syntax.test.

# RUN: yaml2obj %s -o %t.o

## Check that --regex and --wildcard cannot be used together.
# RUN: not llvm-objcopy --regex --wildcard %t.o %t.err.o 2>&1 \
# RUN:   | FileCheck %s --check-prefix=ERR
# RUN: not llvm-strip --regex --wildcard %t.o -o %t.err.o 2>&1 \
# RUN:   | FileCheck %s --check-prefix=ERR

# ERR: error: --regex and --wildcard are incompatible

## Check that section removal flags default to glob matches.

## --keep-section:
# RUN: llvm-objcopy --strip-all --keep-section='.f*' %t.o %t.ksec.1.o
# RUN: llvm-readobj --sections %t.ksec.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,SEC,FOO-SEC
# RUN: llvm-strip --strip-all --keep-section='.f*' %t.o -o %t.ksec.2.o
# RUN: cmp %t.ksec.1.o %t.ksec.2.o

## --only-section:
# RUN: llvm-objcopy --strip-all --only-section='.f*' %t.o %t.osec.1.o
# RUN: llvm-readobj --sections %t.osec.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,SEC,FOO-SEC

## --remove-section:
# RUN: llvm-objcopy --strip-debug --remove-section='.s??tab' %t.o %t.rsec.1.o
# RUN: llvm-readobj --sections %t.rsec.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,SEC,FOO-SEC,BAR-SEC
# RUN: llvm-strip --strip-debug --remove-section='.s??tab' %t.o -o %t.rsec.2.o
# RUN: cmp %t.rsec.1.o %t.rsec.2.o

## Check that symbol removal options default to literal matches. Adding -w
## enables glob support for these options.

## --globalize-symbol:
# RUN: llvm-objcopy --globalize-symbol='*' %t.o %t.globsym.1.o
# RUN: llvm-readobj --symbols %t.globsym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM

# RUN: llvm-objcopy -w --globalize-symbol='*' %t.o %t.globsym.2.o
# RUN: llvm-readobj --symbols %t.globsym.2.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM

## --keep-symbol:
# RUN: llvm-objcopy --discard-all --keep-symbol='f*' %t.o %t.ksym.1.o
# RUN: llvm-readobj --symbols %t.ksym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK
# RUN: llvm-strip --discard-all --keep-symbol='f*' %t.o -o %t.ksym.2.o
# RUN: cmp %t.ksym.1.o %t.ksym.2.o

# RUN: llvm-objcopy --discard-all -w --keep-symbol='f*' %t.o %t.ksym.3.o
# RUN: llvm-readobj --symbols %t.ksym.3.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM
# RUN: llvm-strip --discard-all -w --keep-symbol='f*' %t.o -o %t.ksym.4.o
# RUN: cmp %t.ksym.3.o %t.ksym.4.o

## --keep-symbols:
# RUN: echo 'f*' > %t-fstar.txt
# RUN: llvm-objcopy --discard-all --keep-symbols=%t-fstar.txt %t.o %t.ksym.5.o
# RUN: cmp %t.ksym.5.o %t.ksym.1.o

# RUN: llvm-objcopy --discard-all -w --keep-symbols=%t-fstar.txt %t.o %t.ksym.6.o
# RUN: cmp %t.ksym.6.o %t.ksym.3.o

## --localize-symbol:
## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global.
# RUN: llvm-objcopy --localize-symbol='*' %t.globsym.2.o %t.localsym.1.o
# RUN: llvm-readobj --symbols %t.localsym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM

# RUN: llvm-objcopy -w --localize-symbol='*' %t.globsym.2.o %t.localsym.2.o
# RUN: llvm-readobj --symbols %t.localsym.2.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM

## --localize-symbols:
# RUN: echo '*' > %t-star.txt
# RUN: llvm-objcopy --localize-symbols=%t-star.txt %t.globsym.2.o %t.localsym.3.o
# RUN: cmp %t.localsym.3.o %t.localsym.1.o

# RUN: llvm-objcopy -w --localize-symbols=%t-star.txt %t.globsym.2.o %t.localsym.4.o
# RUN: cmp %t.localsym.4.o %t.localsym.2.o

## --strip-symbol:
# RUN: llvm-objcopy --strip-symbol='f*' %t.o %t.stripsym.1.o
# RUN: llvm-readobj --symbols %t.stripsym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM,BAR-SYM
# RUN: llvm-strip --strip-symbol='f*' %t.o -o %t.stripsym.2.o
# RUN: cmp %t.stripsym.1.o %t.stripsym.2.o

# RUN: llvm-objcopy -w --strip-symbol='f*' %t.o %t.stripsym.3.o
# RUN: llvm-readobj --symbols %t.stripsym.3.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR-SYM
# RUN: llvm-strip -w --strip-symbol='f*' %t.o -o %t.stripsym.4.o
# RUN: cmp %t.stripsym.3.o %t.stripsym.4.o

## --strip-symbols:
# RUN: llvm-objcopy --strip-symbols=%t-fstar.txt %t.o %t.stripsym.5.o
# RUN: cmp %t.stripsym.5.o %t.stripsym.1.o

# RUN: llvm-objcopy -w --strip-symbols=%t-fstar.txt %t.o %t.stripsym.6.o
# RUN: cmp %t.stripsym.6.o %t.stripsym.3.o

## --strip-unneeded-symbol:
# RUN: llvm-objcopy --strip-unneeded-symbol='f*' %t.o %t.stripunsym.1.o
# RUN: llvm-readobj --symbols %t.stripunsym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,FOO-SYM,BAR-SYM

# RUN: llvm-objcopy -w --strip-unneeded-symbol='f*' %t.o %t.stripunsym.2.o
# RUN: llvm-readobj --symbols %t.stripunsym.2.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: --check-prefixes=CHECK,BAR-SYM

## --strip-unneded-symbols:
# RUN: llvm-objcopy --strip-unneeded-symbols=%t-fstar.txt %t.o %t.stripunsym.3.o
# RUN: cmp %t.stripunsym.3.o %t.stripunsym.1.o

# RUN: llvm-objcopy -w --strip-unneeded-symbols=%t-fstar.txt %t.o %t.stripunsym.4.o
# RUN: cmp %t.stripunsym.4.o %t.stripunsym.2.o

## --weaken-symbol:
## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global.
# RUN: llvm-objcopy --weaken-symbol='*' %t.globsym.2.o %t.weaksym.1.o
# RUN: llvm-readobj --symbols %t.weaksym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM

# RUN: llvm-objcopy -w --weaken-symbol='*' %t.globsym.2.o %t.weaksym.2.o
# RUN: llvm-readobj --symbols %t.weaksym.2.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,WEAK,FOO-SYM,BAR-SYM

## --weaken-symbols:
# RUN: llvm-objcopy --weaken-symbols=%t-star.txt %t.globsym.2.o %t.weaksym.3.o
# RUN: cmp %t.weaksym.3.o %t.weaksym.1.o

# RUN: llvm-objcopy -w --weaken-symbols=%t-star.txt %t.globsym.2.o %t.weaksym.4.o
# RUN: cmp %t.weaksym.4.o %t.weaksym.2.o

## --keep-global-symbol:
## Note: Use %t.globsym.2.o instead of %t.o since those symbols are global.
# RUN: llvm-objcopy --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.1.o
# RUN: llvm-readobj --symbols %t.keepgsym.1.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,LOCAL,FOO-SYM,BAR-SYM

# RUN: llvm-objcopy -w --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.2.o
# RUN: llvm-readobj --symbols %t.keepgsym.2.o \
# RUN:   | FileCheck %s --implicit-check-not=Name: \
# RUN:       --check-prefixes=CHECK,GLOBAL,FOO-SYM,BAR-SYM

## --keep-global-symbols:
# RUN: llvm-objcopy --keep-global-symbols=%t-star.txt %t.globsym.2.o %t.keepgsym.3.o
# RUN: cmp %t.keepgsym.3.o %t.keepgsym.1.o

# RUN: llvm-objcopy -w --keep-global-symbols=%t-star.txt %t.globsym.2.o %t.keepgsym.4.o
# RUN: cmp %t.keepgsym.4.o %t.keepgsym.2.o

## Check that -w is accepted as an alias for --wildcard.
# RUN: llvm-objcopy --wildcard --keep-global-symbol='*' %t.globsym.2.o %t.keepgsym.5.o
# RUN: cmp %t.keepgsym.2.o %t.keepgsym.5.o

# CHECK:   LoadName:
# CHECK:   Name: (0)

# FOO-SEC: Name: .foo

# FOO-SYM: Name: foo
# GLOBAL:  Binding: Global
# WEAK:    Binding: Weak
# LOCAL:   Binding: Local

# BAR-SEC: Name: .bar
# BAR-SYM: Name: bar
# GLOBAL:  Binding: Global
# WEAK:    Binding: Weak
# LOCAL:   Binding: Local

# SEC:     Name: .shstrtab

!ELF
FileHeader:
  Class:     ELFCLASS64
  Data:      ELFDATA2LSB
  Type:      ET_REL
  Machine:   EM_X86_64
Sections:
  - Name:    .foo
    Type:    SHT_PROGBITS
  - Name:    .bar
    Type:    SHT_PROGBITS
Symbols:
  - Name:    foo
    Type:    STT_FUNC
    Section: .foo
  - Name:    bar
    Type:    STT_FUNC
    Section: .foo
